<?php

// +----------------------------------------------------------------------
// |Author: 翼小菜
// +----------------------------------------------------------------------
// |Description: 资源服务逻辑层
// +----------------------------------------------------------------------
// |更多功能可联系QQ314688769
// +----------------------------------------------------------------------
namespace api\cveadmin\service;
use api\cveadmin\model\AssetModel;
use api\cveadmin\model\OptionModel;
class AssetService
{
    /** 
     * @description: 统计资源数
     * @return int
     */    
    public function countAssets()
    {
        $count = AssetModel::count();
        return $count;
    }

    /** 
     * @description: 根据传入类型获取资源
     * @param string $data['type'] all|video|image|audio|file $data['subtype'] 用于后缀查询如jpg 
     * @return {*}
     */    
    public function getAssetsByType($data)
    {
        $optionModel = new OptionModel();
        $assetModel = new AssetModel();
        if(empty($data['limit'])){
            $data['limit'] = 1;
        }
   
        // 如果不是all就是根据类型分类
        if($data['type'] != 'all'){
            // 如果传递子类型代表查询同一种后缀文件
            if($data['subtype'] != ''){
                $result = $assetModel->where('suffix',$data['subtype'])->paginate($data['limit'])->toArray();
            }else{
                // 否则 根据 image|video|audio|file 查询
                // 首先找出以上上传配置类型
                $option = $optionModel->where('option_name','upload_setting')->find()->toArray();
                $typesArr = json_decode($option['option_value'],true);
               
                $result = $assetModel->where('suffix','in',$typesArr['file_types'][$data['type']]['extensions'])->paginate($data['limit'])->toArray();
            }
           
        }else{
            $result= $assetModel->paginate($data['limit'])->toArray();
        }
        return $result;    
    }
}